﻿using System;
namespace homework1
{
    public abstract class Shape
    {
        public abstract double Area();
        public abstract bool IsValid();
    }
    public class Rectangle : Shape
    {
        public double Length { set; get; }
        public double Width;

        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public override double Area()
        {
            return Length * Width;
        }

        public override bool IsValid()
        {
            return Length > 0 && Width > 0;
        }
    }
    public class Square : Rectangle
    {
        public Square(double side) : base(side, side) { }
        public override bool IsValid() 
        {
            return Length > 0 && Width > 0 && Length == Width;
        }
    }
    public class Circle : Shape
    {
        public double Radius;

        public Circle(double radius)
        {
            Radius = radius;
        }

        public override double Area()
        {
            return Math.PI * Radius * Radius;
        }

        public override bool IsValid() 
        {
            return Radius > 0;
        } 
    }
    public class Triangle : Shape
    {
        public double a,b,c;

        public Triangle(double _a,double _b,double _c)
        {
            a = _a;
            b = _b; 
            c = _c;
        }

        public override double Area()
        { 
            double s = (a + b + c) / 2;

            double area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));

            return area;
        }

        public override bool IsValid()
        {
            if (a + b <= c || a + c <= b || b + c <= a||a<0||b<0||c<0)
            {
                return false; 
            }
            else return true;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            double totalArea = 0;
            double minValue = 0;  // Set min value  
            double maxValue = 10.0;   // Set max value 

            for (int i = 0; i < 10; i++)
            {
                int shapeType = rand.Next(0, 4); // Randomly choose a shape type  
                Shape shape = null;

                // Create random shapes  
                switch (shapeType)
                {
                    case 0: // Rectangle  
                        // Randomly generate a floating-point number
                        double length = minValue + (rand.NextDouble() * (maxValue - minValue));
                        double width = minValue + (rand.NextDouble() * (maxValue - minValue));
                        shape = new Rectangle(length, width);
                        break;
                    case 1: // Square  
                        double side = minValue + (rand.NextDouble() * (maxValue - minValue));
                        shape = new Square(side);
                        break;
                    case 2: // Circle  
                        double radius = minValue + (rand.NextDouble() * (maxValue - minValue));
                        shape = new Circle(radius);
                        break;
                    case 3: //Triangle
                        double a = minValue + (rand.NextDouble() * (maxValue - minValue));
                        double b = minValue + (rand.NextDouble() * (maxValue - minValue));
                        double c = minValue + (rand.NextDouble() * (maxValue - minValue));
                        shape = new Triangle(a, b, c);
                        break;
                }

                if (shape != null && shape.IsValid())
                {
                    totalArea += shape.Area();
                    Console.WriteLine($"Created: {shape.GetType().Name}, Area: {shape.Area():F2}");
                }
                else if(shape != null && !shape.IsValid() && shape.GetType() == typeof(Square))
                {
                    Square square = shape as Square;
                    Console.WriteLine($"Created: Square, width: {square.Width:F2},length:{square.Length:F2}, The Shape is invalid");
                }
                else if (shape != null && !shape.IsValid() && shape.GetType() == typeof(Rectangle))
                {
                    Rectangle rectangle = shape as Rectangle;
                    Console.WriteLine($"Created: Rectangle, width: {rectangle.Width:F2},length:{rectangle.Length:F2}, The Shape is invalid");
                }
                else if (shape != null && !shape.IsValid() && shape.GetType()==typeof(Circle))
                {
                    Circle circle = shape as Circle;
                    Console.WriteLine($"Created: Circle, radius: {circle.Radius:F2}, The Shape is invalid");
                }
                else if (shape != null && !shape.IsValid() && shape.GetType() == typeof(Triangle))
                {
                    Triangle triangle = shape as Triangle;
                    Console.WriteLine($"Created: Triangle, a: {triangle.a:F2},b: {triangle.b:F2},c: {triangle.c:F2}, The Shape is invalid");
                }
            }

            Console.WriteLine($"Total Area of All Shapes: {totalArea:F2}");
        }
    }

}
